package com.bestcem.xm.user.dao.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.bestcem.xm.user.dao.UserUserRoleDao;
import com.bestcem.xm.user.dao.mapper.UserUserRoleMapper;
import com.bestcem.xm.user.entity.mysql.UserUserRole;
import com.bestcem.xm.user.entity.pojo.UserUserRoleDO;
import com.bestcem.xm.user.util.business.DataBaseUtil;
import com.bestcem.xm.user.util.convert.UserUserRoleConvert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户与角色的关联业务
 *
 * @author wangyunyun <yunyun.wang@idiaoyan.com>
 * @version v1.0
 * @date 2023/3/4 14:30
 */
@Repository
public class UserUserRoleDaoImpl implements UserUserRoleDao {

    @Resource
    private UserUserRoleConvert userUserRoleConvert;

    @Resource
    private UserUserRoleMapper userUserRoleMapper;

    @Override
    public List<UserUserRoleDO> listByCondition(UserUserRoleDO userUserRoleDO) {
        return userUserRoleConvert.mysqls2Dos(userUserRoleMapper.selectBySelective(userUserRoleConvert.do2Mysql(userUserRoleDO)));
    }

    @Override
    public String insertSelective(UserUserRoleDO userUserRoleDO) {
        UserUserRole userUserRole = userUserRoleConvert.do2Mysql(userUserRoleDO);
        DataBaseUtil.setCreateAndUpdateTime(userUserRole);
        userUserRole.setUserRoleId(DataBaseUtil.generateId());
        userUserRoleMapper.insertSelective(userUserRole);
        return userUserRoleDO.getUserRoleId();
    }

    /**
     * 根据userId删除用户与角色的关联关系
     *
     * @param userId
     * @return com.bestcem.xm.util.ServiceResult<java.lang.Object>
     * @author panyun <yun.pan@idiaoyan.com>
     * @date 2022/3/9 10:37
     */
    @Override
    public int deleteByUserId(String userId) {
        return userUserRoleMapper.deleteByUserId(userId);
    }

    @Override
    public Integer deleteByRoleIdAndUserIds(String orgId, String roleId, List<String> userIds) {
        return userUserRoleMapper.deleteByRoleIdAndUserIds(orgId, roleId, userIds);
    }

    /**
     * 批量插入
     *
     * @param userRoles
     * @return int
     * @author panyun <yun.pan@idiaoyan.com>
     * @date 2022/3/9 13:56
     */
    @Override
    public int insertUserRoleList(List<UserUserRole> userRoles) {
        for (UserUserRole userUserRole : userRoles) {
            DataBaseUtil.setCreateAndUpdateTime(userUserRole);
        }
        return userUserRoleMapper.insertUserRoleList(userRoles);
    }

    /**
     * 根据roleIdList获取userIdList
     *
     * @param roleIdsList
     * @return java.util.List<java.lang.String>
     * @author panyun <yun.pan@idiaoyan.com>
     * @date 2022/3/14 17:18
     */
    @Override
    public List<String> listByRoleIds(List<String> roleIdsList) {
        List<String> strings = userUserRoleMapper.listByRoleIds(roleIdsList);
        if(CollectionUtil.isNotEmpty(strings)){
            return strings.stream().filter(StringUtils::isNotBlank).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public List<String> listRoleIdByUserId(String userId) {
        return userUserRoleMapper.listRoleIdByUserId(userId);
    }

    @Override
    public Integer updateSelectiveById(UserUserRoleDO userUserRoleDO) {
        UserUserRole userUserRole = userUserRoleConvert.do2Mysql(userUserRoleDO);
        DataBaseUtil.setUpdateTime(userUserRole);
        return userUserRoleMapper.updateByPrimaryKeySelective(userUserRole);
    }

    @Override
    public Integer deleteByRoleId(String roleId) {
        return userUserRoleMapper.deleteByRoleId(roleId);
    }

    @Override
    public Integer deleteByRoleIdList(String userId, List<String> roleIdList) {
        return userUserRoleMapper.deleteByRoleIdList(userId, roleIdList);
    }
}

